﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WMS_IDAL;

namespace WMS_BLL
{
    public abstract class BaseBLL<T> where T : class, new()
    {
        public IBaseIDAL<T>? currentdal { get; set; }
        public async Task<bool> CreateAsync(T entity)
        {
            if (currentdal != null)
            {
                return await currentdal.CreateAsync(entity);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<bool> EditAsync(T entity)
        {
            if (currentdal != null)
            {
                return await currentdal.EditAsync(entity);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<T> FindAsync(int id)
        {
            if (currentdal != null)
            {
                return await currentdal.FindAsync(id);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<int> FastInsertRangeAsync(List<T> list)
        {
            if (currentdal != null)
            {
                return await currentdal.FastInsertRangeAsync(list);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<int> BulkUpdateAsync(List<T> list)
        {
            if (currentdal != null)
            {
                return await currentdal.BulkUpdateAsync(list);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<List<T>> QueryListAsync(Expression<Func<T, bool>> whereLambda)
        {
            if (currentdal != null)
            {
                return await currentdal.QueryListAsync(whereLambda);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<int> DeleteRangeAsync(Expression<Func<T, bool>> whereLambda)
        {
            if (currentdal != null)
            {
                return await currentdal.DeleteRangeAsync(whereLambda);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<bool> DeleteByIdAsync(int id)
        {
            if (currentdal != null)
            {
                return await currentdal.DeleteByIdAsync(id);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<List<T>> BlockTranAsync(Expression<Func<T, bool>> whereLambda)
        {
            if (currentdal != null)
            {
                return await currentdal.BlockTranAsync(whereLambda);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<bool> QueryListIsExistAsync(Expression<Func<T, bool>> whereLambda)
        {
            if (currentdal != null)
            {
                return await currentdal.QueryListIsExistAsync(whereLambda);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<List<T>> ToPageListAsync(int PageNow, int PageSize)
        {
            if (currentdal != null)
            {
                return await currentdal.ToPageListAsync(PageNow, PageSize);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
        public async Task<List<T>> ToWherePageListAsync(int PageNow, int PageSize, Expression<Func<T, bool>> whereLambda)
        {
            if (currentdal != null)
            {
                return await currentdal.ToWherePageListAsync(PageNow, PageSize,whereLambda);
            }
            else
            {
                var classname = typeof(T).Name.Split('_');//获得T的类型名
                var content = classname[classname.Length - 1];
                throw new Exception($"类型{content}构造异常");
            }
        }
    }
}
